<?php
/**
 * Created by PhpStorm.
 * User: zhanglin02
 * Date: 2016/3/31
 * Time: 16:52
 */

namespace Admin\Controller;
use Think\Controller;

class LoginController extends Controller
{//登录页
    public function index(){
        if(IS_POST){
            //验证码
            if($this->checkVerify(I('post.verify')) ==false){
                $this->error('验证码错误',U('Login/index'));
            }
            $Admin = D('Admin');//实例化模型
            if(!$Admin->create()){
                $this->error('登陆失败：'.$Admin->getError(),U('Login/index'));
            }
            $username = $Admin->username;//获取用户名
            if($Admin->checkLogin()){
                //登陆成功后，将用户名保存到Session并跳转到index
                session('userinfo',array('name'=>$username));
                $this->redirect('Index/index');
            }
            $this->error("登陆失败：用户名或密码错误",U('Login/index'));
        }
        $this->display();
    }
    //生成验证码
    public function getVerify(){
        $Verify = new \Think\Verify();
        $Verify->entry();
    }
    //检查验证码
    private function checkVerify($code,$id = ''){
        $Verify = new \Think\Verify();
        return $Verify->check($code,$id);
    }
    public function logout(){
        session(null);//清空session
        $this->redirect('Login/index');
    }
    public function _empty($name){
        $this->error('无效的操作：'.$name);
    }
}